package com.xdf.whiteaccount.controller;

import com.xdf.whiteaccount.dto.PlanDTO;
import com.xdf.whiteaccount.entity.ResponseResult;
import com.xdf.whiteaccount.enums.*;
import com.xdf.whiteaccount.service.CallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.xdf.whiteaccount.entity.ProducePlan;
import com.xdf.whiteaccount.service.ProducePlanService;
import com.xdf.whiteaccount.utils.JsonResult;

/**
 * 注意要有依赖：
 *         <dependency>
 *             <groupId>io.springfox</groupId>
 *             <artifactId>springfox-swagger2</artifactId>
 *             <version>2.9.2</version>
 *         </dependency>
 *         <dependency>
 *             <groupId>io.springfox</groupId>
 *             <artifactId>springfox-swagger-ui</artifactId>
 *             <version>2.9.2</version>
 *         </dependency>
 * 同时入口类头上使用注解：@EnableSwagger2
 * @Description : 控制层，代码生成器自动生成
 * @Author : chanchaw
 * @Date : 2025-3-4 10:56:34
 */
@Api(tags = "生产计划")
@CrossOrigin
@RestController
@RequestMapping("/producePlan")
public class ProducePlanController {
    @Autowired
    private ProducePlanService service;
    @Autowired
    private CallService callService;

    // 全部属性新增
    @ApiOperation(value = "完全新增", httpMethod = "POST", notes = "推荐使用save请求")
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody ProducePlan record){
        return JsonResult.ok(service.insert(record));
    }

    // 根据主键删除，切记表要有主键，否则为删除所有数据！！！！！！！！！！
    @ApiOperation(value = "根据主键删除", httpMethod = "POST", notes = "所有请求都是post")
    @PostMapping("/delete/{pk}")
    public JsonResult deleteByPrimaryKey(@PathVariable Integer pk){
        return JsonResult.ok(service.deleteByPrimaryKey(pk));
    }

    // 根据主键查询，切记表要有主键，否则为查询所有数据！！！！！！！！！！
    @ApiOperation(value = "根据主键查询", httpMethod = "POST", notes = "所有请求都是post")
    @PostMapping("/select/{pk}")
    public JsonResult selectByPrimaryKey(@PathVariable Integer pk) throws Exception {
        return JsonResult.ok(service.selectByPrimaryKey(pk));
    }

    // 获取所有数据
    @ApiOperation(value = "查询所有", httpMethod = "POST", notes = "所有请求都是post")
    @PostMapping("/selectAll")
    public JsonResult selectAll(){
        return JsonResult.ok(service.selectAll());
    }

    // 全部属性更新。前端传递来属性value=null则会设置后台数据为null
    @ApiOperation(value = "完全修改", httpMethod = "POST", notes = "推荐使用save方法")
    @PostMapping("/updateByPrimaryKey")
    public JsonResult updateByPrimaryKey(@RequestBody ProducePlan record) {
        return JsonResult.ok(service.updateByPrimaryKey(record));
    }

    // 部分属性更新。前端传递来value=null的属性不会被更新到数据库
    @ApiOperation(value = "选择性修改", httpMethod = "POST", notes = "推荐使用save方法")
    @PostMapping("/updateByPrimaryKeySelective")
    public JsonResult updateByPrimaryKeySelective(@RequestBody ProducePlan record) {
        return JsonResult.ok(service.updateByPrimaryKeySelective(record));
    }

    // 有则修改，无则新增。选择性保存，如果属性value=null则不会更新该字段
    @ApiOperation(value = "保存数据", httpMethod = "POST", notes = "主键为空则新增(选择性新增)，主键有值则修改（选择性修改）")
    @PostMapping("/save")
    public JsonResult save(@RequestBody ProducePlan record) {
        return JsonResult.ok(service.save(record));
    }

    // 是上面接口 save 的批量执行版本
    @ApiOperation(value = "对象数组保存", httpMethod = "POST", notes = "所有请求都是post")
    @PostMapping("/multiSave")
    public JsonResult multiSave(@RequestBody List<ProducePlan> records) {
        return JsonResult.ok(service.multiSave(records));
    }
    //=================以上是自动生成的代码，自定义代码请写在下面==================

    /**
     * @Description : 查询报表
     * @Return : ResponseResult<Void>
     * @Author : 张柯
     * @Date : 2021-05-16 09:40:12
     */
    @ApiOperation(value = "序时表", httpMethod = "GET/POST", notes = "序时表")
    @RequiresPermissions(MenuConstant.MENU103 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @RequestMapping(value = "/report")
    public List<Map<String, Object>> getPlanReport(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                                                   @DateTimeFormat(pattern = "yyyy-MM-dd") Date end,
                                                   Integer stateNumber) throws Exception {
        return callService.getProducePlanReport(start, end, stateNumber);
    }

    /**
     * @Description : 选择修改
     * @Return : Plan
     * @Author : 张柯
     * @Date : 2021-05-16 09:40:12
     */
    @ApiOperation(value = "选择修改", httpMethod = "PUT", notes = "自动生成的修改方法")
    @RequiresPermissions(MenuConstant.MENU103 + SeparatorConstant.SEPARATOR + OperateConstant.MODIFY)
    @RequestMapping(value = "/updateSelective", method = RequestMethod.POST)
    public ResponseResult<ProducePlan> updateSelective(@Validated @RequestBody ProducePlan record) throws Exception {
        ProducePlan result = service.updateSelective(record);
        return ResponseResult.<ProducePlan>builder().success(true).message(ResponseEnum.OK.getName()).data(result).build();
    }

    /**
     * @Description : 选择新增
     * @Return : ResponseResult<Void>
     * @Author : 张柯
     * @Date : 2021-05-16 09:40:12
     */
    @ApiOperation(value = "选择新增", httpMethod = "POST", notes = "自动生成的新增方法")
    @RequiresPermissions(MenuConstant.MENU103 + SeparatorConstant.SEPARATOR + OperateConstant.ADD)
    @RequestMapping(value = "/insertSelective", method = RequestMethod.POST)
    public ResponseResult<ProducePlan> insertSelective(@Validated @RequestBody ProducePlan record) throws Exception {
        ProducePlan result = service.insertSelective(record);
        return ResponseResult.<ProducePlan>builder().success(true).message(ResponseEnum.OK.getName()).data(result).build();
    }

    /**
     * @Description : 根据主键查询计划全部信息
     * @Return : Plan
     * @Author : 张柯
     * @Date : 2021-05-16 09:40:12
     */
    @ApiOperation(value = "根据主键查询", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequiresPermissions(MenuConstant.MENU103 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @RequestMapping(value = "/queryDtoByPk/{id}", method = RequestMethod.GET)
    public ProducePlan selectDtoByPrimaryKey(@PathVariable("id") Integer id) throws Exception {
        if (id == null) return null;
        ProducePlan producePlan = service.selectByPrimaryKey(id);
        return producePlan;
    }

    /**
     * 审核所有
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "审核所有", httpMethod = "GET", notes = "审核所有")
    @RequiresRoles(value = RoleConstant.ADMIN_ROLE)
    @PostMapping("/auditAll")
    public ResponseResult<Void> auditAll() throws Exception {
        service.auditAll();
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    /**
     * @Description : 设置完工状态
     * @Return : java.utils.List
     * @Author : 张柯
     * @Date : 2021-05-16 09:40:12
     */
    @ApiOperation(value = "设置完工状态", httpMethod = "POST", notes = "自动生成的查询方法")
    @RequiresPermissions(MenuConstant.MENU103 + SeparatorConstant.SEPARATOR + OperateConstant.MODIFY)
    @RequestMapping(value = "/finished", method = RequestMethod.POST)
    public ResponseResult<Void> setFinishedState(@RequestParam("id") Integer id) throws Exception {
        service.setFinishedMark(id);
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    /**
     * @Description : 设置完工状态
     * @Return : java.utils.List
     * @Author : 张柯
     * @Date : 2021-05-16 09:40:12
     */
    @ApiOperation(value = "设置完工状态", httpMethod = "POST", notes = "自动生成的查询方法")
    @RequiresPermissions(MenuConstant.MENU103 + SeparatorConstant.SEPARATOR + OperateConstant.MODIFY)
    @RequestMapping(value = "/nofinished", method = RequestMethod.POST)
    public ResponseResult<Void> setNoFinishedState(@RequestParam("id") Integer id) throws Exception {
        service.setNoFinishedState(id);
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    /**
     * @Description : 根据主键查询计划全部信息
     * @Return : Plan
     * @Author : 张柯
     * @Date : 2021-05-16 09:40:12
     */
    @ApiOperation(value = "根据主键查询", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequiresPermissions(MenuConstant.MENU103 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @RequestMapping(value = "/selectMax", method = RequestMethod.GET)
    public ProducePlan selectMax() throws Exception {
        ProducePlan producePlan = service.selectMax();
        return producePlan;
    }


    /**
     * @Description : 获取即将生成的订单号
     * @Return : java.utils.List
     * @Author : 张柯
     * @Date : 2021-05-16 09:40:12
     */
    @ApiOperation(value = "获取即将生成的订单号", httpMethod = "POST", notes = "自动生成的查询方法")
    @RequiresPermissions(MenuConstant.MENU103 + SeparatorConstant.SEPARATOR + OperateConstant.MODIFY)
    @RequestMapping(value = "/maxOrderCode", method = RequestMethod.POST)
    public ResponseResult<String> maxOrderCode() throws Exception {
        String s = service.maxOrderCode();
        return ResponseResult.<String>builder().data(s).success(true).message(ResponseEnum.OK.getName()).build();
    }

    /**
     * @Description : 查询明细
     * @Return : ResponseResult<Void>
     * @Author : 张柯
     * @Date : 2021-05-16 09:40:12
     */
    @ApiOperation(value = "查询明细", httpMethod = "GET/POST", notes = "查询明细")
    @RequiresPermissions(MenuConstant.MENU103 + SeparatorConstant.SEPARATOR + OperateConstant.SHOW)
    @RequestMapping(value = "/selectByKCMX/{id}")
    public List<Map<String, Object>> selectByKCMX(@PathVariable("id") Integer id) throws Exception {
        List<Map<String, Object>> list = service.selectByKCMX(id);
        if (list.size() == 0) throw new IllegalStateException("未检测到相关明细");
        return list;
    }
}